num = int(input())

def filt(el):
	el = int(el)
	if findM(el) == el:
		return True
	else:
		return False

def func():
	if len(str(num)) != 4:
		print(0)
		return
		
	nums = [*str(num)]
	for n in range(4):
		for n1 in str(num)[n + 1:]:
			nums.append(str(num)[n] + n1)
	for n in range(6):
			for n1 in str(num)[n + 2:]:
				nums.append(nums[n] + n1)
	for n in range(4, 9):
			for n1 in str(num)[n - 2:]:
				nums.append(nums[n] + n1)
	nums.append(num)
	
	nums = filter(filt, nums)
		
	nums = list(map(int, nums))
	# print(nums)
	if not nums:
		print(0)
		return
	if not (m := max(nums)):
		print(0)
	else:
		print(m)


def findM(num):
	if num == 1:
		return num
	x = 2
	while x != num // 2 + 1:
		if not num % x:
			if findM(x) == x:
				return x
		x += 1
	return num


func()
